//
//  ZhiMaViewController.m
//  51易借
//
//  Created by apple on 2017/9/18.
//  Copyright © 2017年 张睿. All rights reserved.
//

#import "ZhiMaViewController.h"

@interface ZhiMaViewController ()<UIWebViewDelegate>
@property (nonatomic,strong)UIWebView * web;
@property (nonatomic,strong)UIView * topView;
@property (nonatomic,assign)BOOL isLoadingFinished;
@end

@implementation ZhiMaViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self setupNavBar];
    [self setupUI];
    [self updateUI];
    // Do any additional setup after loading the view.
}



-(void)updateUI
{
    self.title = @"芝麻信用";
//    self.isLoadingFinished = NO;
    
//    self.automaticallyAdjustsScrollViewInsets = NO;
    self.web = [[UIWebView alloc] initWithFrame:CGRectMake(0, 70 * SCREENH6, SCREEN_WIDTH, SCREENH_HEIGHT-70 * SCREENH6)];
    self.web.scalesPageToFit = YES;
//    self.web.scrollView.contentSize = self.web.bounds.size;
//    self.web.scrollView.bounces = NO;
//    self.web.scrollView.showsVerticalScrollIndicator = YES;
//    self.web.scrollView.showsHorizontalScrollIndicator = NO;
    self.web.delegate = self;
//    [self.web sizeToFit];
    [self.view addSubview:self.web];
    
    [self.web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.url]]];
    
//    [self.web loadHTMLString:self.url baseURL:nil];
    //第一次加载先隐藏webview
//    [self.web setHidden:YES];
}

- (void)webViewDidFinishLoad:(UIWebView*)webView {
    
    
//    [self.web stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '125%'"];
    
//    [self.web ];
    
//   NSString * javascript = [NSString stringWithFormat:@"var viewPortTag=document.createElement('meta');  \
//                  viewPortTag.id='viewport';  \
//                  viewPortTag.name = 'viewport';  \
//                  viewPortTag.content = 'width=%d; initial-scale=1.0; maximum-scale=0.1; user-scalable=0;';  \
//                  viewPortTag.content = 'height=%d; initial-scale=1.0; maximum-scale=0.35; user-scalable=0;';  \
//                  document.getElementsByTagName('head')[0].appendChild(viewPortTag);" , (int)webView.bounds.size.width,(int)webView.bounds.size.height];
//
//    [webView stringByEvaluatingJavaScriptFromString:javascript];
//
//    webView.scalesPageToFit = YES;
    
//    若已经加载完成，则显示webView并return
//    if(self.isLoadingFinished)
//    {
//        [self.web setHidden:NO];
//        return;
//    }
    
    //js获取body宽度
//    NSString *bodyWidth= [webView stringByEvaluatingJavaScriptFromString: @"document.body.scrollWidth "];
//    
//    int widthOfBody = [bodyWidth intValue];
//    
//    //获取实际要显示的html
//    NSString *html = [self htmlAdjustWithPageWidth:widthOfBody
//                                              html:currentMail.htmlBody
//                                           webView:webView];
//    
//    //设置为已经加载完成
//    self.isLoadingFinished = YES;
//    //加载实际要现实的html
//    [self.web loadHTMLString:html baseURL:nil];
//    [self.web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.url]]];
}

//获取宽度已经适配于webView的html。这里的原始html也可以通过js从webView里获取
- (NSString *)htmlAdjustWithPageWidth:(CGFloat )pageWidth
                                 html:(NSString *)html
                              webView:(UIWebView *)webView
{
    NSMutableString *str = [NSMutableString stringWithString:html];
    //计算要缩放的比例
    CGFloat initialScale = webView.frame.size.width/pageWidth;
    //将</head>替换为meta+head
    NSString *stringForReplace = [NSString stringWithFormat:@"<meta name=\"viewport\" content=\" initial-scale=%f, minimum-scale=0.1, maximum-scale=2.0, user-scalable=yes\"></head>",initialScale];
    
    NSRange range =  NSMakeRange(0, str.length);
    //替换
    [str replaceOccurrencesOfString:@"</head>" withString:stringForReplace options:NSLiteralSearch range:range];
    return str;
}

- (void)setupNavBar{
    
    
    UIButton * backButton = [[UIButton alloc] init];
    
    backButton.frame = CGRectMake(15 * SCREENH6, 5 * SCREENH6, 12 * SCREENH6, 20 * SCREENH6);
    [backButton setImage:[UIImage imageNamed:@"箭头"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backToLastView) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    self.navigationItem.leftBarButtonItem = leftButton;
}



- (void)setupUI{
    
    //    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIView * topView = [UIView new];
    
    topView.backgroundColor = ColorWithRGB(0xff7907);
    
    self.topView = topView;
    
    [self.view addSubview:self.topView];
    //
    
}


- (void)viewDidLayoutSubviews{
    
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.trailing.mas_equalTo(self.view);
        make.height.mas_equalTo(64);
    }];
}

-(void)backToLastView
{
    [self.navigationController popViewControllerAnimated:YES];
}


@end
